﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System.Diagnostics;
using System.Reflection;
using NUnitGrid.Framework.Plugins;
using NUnitGrid.Framework.UI;


namespace NUnitGrid.GridClient.Plugins.GridClient.UI.RunOutput
{
    public class RunOutputElementController : IUIChildPlugin, IRunSessionListener
    {
        private readonly Stopwatch sessionRunTime = new Stopwatch();
        private readonly ITextPanelElement textPanelElement;

        public RunOutputElementController(ITextPanelElement textPanelElement)
        {
            this.textPanelElement = textPanelElement;
        }

        void IRunSessionListener.OnStarted()
        {
            sessionRunTime.Start();
            textPanelElement.Clear();
            textPanelElement.Append("Running tests ...\n");
        }

        void IRunSessionListener.OnCompleted(int passedCount, int failedCount, bool taskError)
        {
            textPanelElement.Append("Completed.\n");

            var success = passedCount > 0 && failedCount == 0 && !taskError;

            textPanelElement.Append("\n");
            textPanelElement.Append(string.Format("Passed: {0}. Failed: {1}\n", passedCount, failedCount));
            textPanelElement.Append(success ? "  Passed\n" : "  F A I L E D\n");

            sessionRunTime.Stop();
            textPanelElement.Append(string.Format("Time taken: {0:00}:{1:00}\n", sessionRunTime.Elapsed.Minutes,
                                                  sessionRunTime.Elapsed.Seconds));

            textPanelElement.ScrollToEnd();
        }

        void IRunSessionListener.OnMesssage(string format, params object[] args)
        {
            textPanelElement.Append(string.Format(format, args));
            textPanelElement.ScrollToEnd();
        }

        void IRunSessionListener.OnStopping()
        {
            textPanelElement.Append("Stopping ...\n");
            textPanelElement.ScrollToEnd();
        }

        void IRunSessionListener.OnTaskCompleted(string fixture, bool succeeded, string nodeMachineName,
                                                 string textOutput)
        {
            ((IRunSessionListener) this).OnMesssage("Fixture '{0}' {1} on '{2}'\n", fixture,
                                                    succeeded ? "passed" : "failed", nodeMachineName);

            if (!succeeded)
            {
                ((IRunSessionListener) this).OnMesssage(textOutput);
            }

            textPanelElement.ScrollToEnd();
        }

        public void Start(IHostView hostView)
        {
            textPanelElement.IsReadOnly = true;
            hostView.AddChild(textPanelElement);

            var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            textPanelElement.Append(string.Format("NUnitGridRunner Client ver {0}\n", version));
        }

        public void Stop(IHostView view)
        {
        }
    }
}